package com.atguigu01.lamda;

import org.junit.Test;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.function.Consumer;

public class lamdaTest {
    @Test
    public void t1(){

        //1 无参，无返回值
        Runnable r1=new Runnable() {
            @Override
            public void run() {
                System.out.println("running");
            }
        };

        /*
        * lamda表达式的写法 确定的东西都可以不写  ()需要写是因为可能出现方法的重载
        * run不需要写是因为接口中只有一个方法
        * */
        Runnable r2=()-> {
            System.out.println("running");
        };
         r2.run();
    }

    @Test
    public void t2(){
        //需要一个参数，但是没有返回值
        Consumer<String> con=new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        };
        con.accept("你好");

        Consumer<String> con1=(String s)->{
            System.out.println(s);
        };

        con.accept("nihao");
    }

    @Test
    public void t3(){
        //数据类型可以省略，因为可由编译器推断得出，称为”类型推断"
        Consumer<String> con=new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        };
        con.accept("你好");

        Consumer<String> con1=(s)->{ //泛型中已经指明了类型
            System.out.println(s);
        };

        con1.accept("nihao");
    }
    @Test
    public void t4(){
        //数据类型可以省略，因为可由编译器推断得出，称为”类型推断"
        Consumer<String> con=new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        };
        con.accept("你好");

        Consumer<String> con1=s->{ //()也可以去掉
            System.out.println(s);
        };

        con.accept("nihao");
    }
    @Test
    public void t5(){
        //Lambda需要两个或以上的参数，多条执行语句，并且可以有返回值
        Comparator<Integer> com1=new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                System.out.println(o1);
                System.out.println(o2);
                return Integer.compare(o1,o2);
            }
        };

        System.out.println(com1.compare(23, 45));

        Comparator<Integer> com2=(o1, o2)->{
            System.out.println(o1);
            System.out.println(o2);
            return Integer.compare(o1,o2);
        };
        System.out.println(com2.compare(23, 12));


    }
    @Test
    public void t6(){
        //当Lambda体只有一条语句时, return与大括号若有，都可以省略
        Comparator<Integer> com1=new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return Integer.compare(o1,o2);
            }
        };

        System.out.println(com1.compare(23, 45));

        Comparator<Integer> com2=(o1, o2)-> Integer.compare(o1,o2);

        System.out.println(com2.compare(23, 12));

        LocalDate localDate= LocalDate.of(2023,12,27);
        LocalDate localDate1=localDate.plusDays(28);
        LocalDate localDate2=localDate.plusDays(30);
        System.out.println(localDate1);
        System.out.println(localDate2);


    }
}
